<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ include file="/common/taglibs.jsp"%>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>系统部门</title>
<link type="text/css" rel="stylesheet"
	href="${ctx }/js/ztree/css/zTreeStyle/zTreeStyle.css" />
<script type="text/javascript" src="${ctx }/js/bootstrap/jquery.js"></script>
<script type="text/javascript">
	$(function() {
		$("#deptForm").validationEngine('attach', {promptPosition : "bottomRight:-60,-20", scroll:false,
			autoHidePrompt: true,autoHideDelay: 5000 });
	});
	
	var setting = {
			view: {
				showIcon: false
			},
			data: {
				simpleData: {
					enable: true
				}
			},
			callback: {
				onClick: addOrEditMenu
			}
		};

		var zNodes = ${deptTree};
		
		var zTree;
		
		$(document).ready(function(){
			$.fn.zTree.init($("#deptTree"), setting, zNodes);
			zTree = $.fn.zTree.getZTreeObj("deptTree");
		});
	
		
		function addOrEditMenu(event, treeId, treeNode, clickFlag){
			$("#deptView").show();
			$("#deptBut").hide();
			
			var id  = treeNode.id;
			$("#deptId").val(id);
			$("#id").val(id);
			
			$.ajax({
				url:"${ctx}/dept/viewDept",
				type:"POST",
				dataType:"text",
				data:"id="+id,
				success:function(data){
					var json = eval("("+data+")");
					
					$("#name").val(json.name);
					$("#rmk").val(json.rmk);
					$("#departmentCode").val(json.departmentCode);
					$("#createUser").val(json.createUser);
					$("#royaltyRate").val(json.royaltyRate);
					var date = new Date(json.createTime);
					$("#createTime").val(date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()+" "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds());
					
					$("#deptCUser").show();
					$("#deptCTime").show();
				}
			});			
		}
		
		function addDept(){
			if($("#deptId").val() == ""){
				art.dialog({content:"请选择一个部门",ok:true});
				return ;
			}
			
			
			$("#name").val("");
			$("#rmk").val("");
			$("#id").val("");
			$("#deptCUser").hide();
			$("#deptCTime").hide();
			
			$("#status").val("1");
			
			$("#deptBut").show();
		}
		
		function updateDept(){
			if($("#deptId").val() == ""){
				art.dialog({content:"请选择一个部门",ok:true});
				return ;
			}
			
			$("#id").val($("#deptId").val());
			
			$.ajax({
				url:"${ctx}/dept/viewDept",
				type:"POST",
				dataType:"text",
				data:"id="+$("#deptId").val(),
				success:function(data){
					var json = eval("("+data+")");
					
					$("#name").val(json.name);
					$("#rmk").val(json.rmk);
				}
			});	
			
			$("#status").val("2");
			$("#deptCUser").hide();
			$("#deptCTime").hide();
			
			$("#deptBut").show();
		}
		
		function subForm(){
			var str = "";
			var url = "";
			var status = $("#status").val();
			
			if(status == "1"){
				str = "新增";
				url = "${ctx}/dept/add";
			}else if(status =="2"){
				str = "修改";
				url = "${ctx}/dept/update";
			}
			
			if($("#deptForm").validationEngine('validate')){
				$.ajax({
					url:"${ctx}/dept/repetitionValidate",
					type:"POST",
					dataType:"text",
					data:"name="+$("#name").val()+"&status="+status+"&updateId="+$("#deptId").val(),
					success:function(data){
						if(data == "0"){
							$('#name').validationEngine('showPrompt', '该部门名已存在，请重新输入！', 'error');
						}else{
							$("#deptForm").ajaxSubmit({
								url:url,
						        type: "post",
						        dataType: "text",
						        success: function(data){
						        	if(data == 'true'){
						        		art.dialog({content:str+"成功！",ok:function(){window.location.href="${ctx}/dept/list";}});
						        	}else if(data == 'DataIntegrityViolation'){
						        		art.dialog({content:"部门编码已关联其他功能，不能修改",ok:true});
						        	}else{
						        		art.dialog({content:str+"失败！",ok:true});
						        	}
						        }
						    });
						}
					}
				});
			}
		}
		
		function deleteDept(){
			var id = $("#deptId").val();
			if(id == ""){
				art.dialog({content:"请选择一个部门",ok:true});
				return ;
			}
			
			art.dialog({
				content:"确认要删除该部门吗？",
				ok:function(){
					$.ajax({
						url:"${ctx}/dept/whetherCanDelete",
						type:"POST",
						dataType:"text",
						data:"id="+id,
						success:function(data){
							if(data == "false"){
								art.dialog({content:"该部门下有人员，不能删除。",ok:true});						
							}else if(data == "true"){
								$.ajax({
									url:"${ctx}/dept/delete",
									type:"POST",
									dataType:"text",
									data:"id="+id,
									success:function(data){
										if(data == 'true'){
							        		art.dialog({content:"删除成功！",ok:function(){window.location.href="${ctx}/dept/list";}});
							        	}else if(data == "root"){
							        		art.dialog({content:"不能删除根节点！",ok:true});
							        	}else{
							        		art.dialog({content:"删除失败！",ok:true});
							        	}
									}
								});
							}
						}
					});
				},
				cancel:function(){
					return ;
				}
			});
			
		}
		
</script>
</head>
<body>
	<div class="breadcrumbs" id="breadcrumbs">
		<script type="text/javascript">
        try{ace.settings.check('breadcrumbs' , 'fixed')}catch(e){}
    </script>

		<ul class="breadcrumb">
			<li><i class="ace-icon fa fa-home home-icon"></i> <a
				href="${ctx }/home/home">首页</a></li>
			<li class="active">系统管理</li>
			<li class="active">部门管理</li>
		</ul>
		<!-- .breadcrumb -->
	</div>

	<div class="page-content">
		<div class="page-header">
			<h1>
				部门管理 <small> <i class="ace-icon fa fa-double-angle-right"></i>
					维护部门
				</small>
				<div style="float: right;">
					<shiro:hasPermission name="dept:add">
						<button type="button" class="btn btn-success btn-sm"
							onclick="addDept()">
							<i class="ace-icon fa fa-plus"></i>新增下级
						</button>
					</shiro:hasPermission>
					<shiro:hasPermission name="dept:update">
						<button type="button" class="btn btn-primary btn-sm"
							onclick="updateDept()">
							<i class="ace-icon fa fa-edit"></i>修改部门
						</button>
					</shiro:hasPermission>
					<shiro:hasPermission name="dept:delete">
						<button type="button" class="btn btn-default btn-sm"
							onclick="deleteDept()">
							<i class="ace-icon fa fa-trash"></i>删除部门
						</button>
					</shiro:hasPermission>
				</div>
			</h1>
		</div>
		<!-- /.page-header -->
		<div class="row">
			<div class="col-xs-4">
				<div class="widget-box">
					<div class="widget-header header-color-blue2">
						<h4 class="lighter smaller">部门</h4>
					</div>
					<div class="widget-body">
						<div class="widget-main padding-8">
							<ul id="deptTree" class="ztree"></ul>
						</div>
					</div>
				</div>
			</div>
			<form id="deptForm" action="" method="post" class="form-horizontal">
				<input type="hidden" id="deptId" name="deptId" /> <input
					type="hidden" id="id" name="id" /> <input type="hidden"
					id="status" name="status" />
				<div class="col-xs-5" style="display: none;" id="deptView">
					<div class="widget-header header-color-blue2"></div>
					<div class="widget-body">
						<div class="widget-main padding-8">
							<div>
								<div class="form-group" id="deptName">
									<label for="inputEmail3" class="col-sm-3 control-label"><b
										class="red">*</b><strong>部门名称</strong></label>
									<div class="col-sm-5">
										<input type="text" class="form-control validate[required]"
											id="name" name="name" maxlength="20">
									</div>
								</div>
								<div class="form-group" id="deptName">
									<label for="inputEmail3" class="col-sm-3 control-label"><b
										class="red">*</b><strong>部门编码</strong></label>
									<div class="col-sm-5">
										<input type="text" class="form-control validate[required]"
											id="departmentCode" name="departmentCode"  maxlength="50">
									</div>
								</div>
								<div class="form-group" id="deptName">
									<label for="inputEmail3" class="col-sm-3 control-label"><b
										class="red">*</b><strong>提成比例</strong></label>
									<div class="col-sm-5">
										<input type="text"
											class="form-control validate[custom[money]]" id="royaltyRate"
											name="royaltyRate"  maxlength="11">
									</div>
								</div>
								<div class="form-group" id="deptRmk">
									<label for="inputEmail3" class="col-sm-3 control-label"><b
										class="red">*</b><strong>备注</strong></label>
									<div class="col-sm-8">
										<input type="text" class="form-control validate[required]"
											id="rmk" name="rmk" maxlength="100">
									</div>
								</div>
								<div class="form-group" id="deptCUser">
									<label for="inputEmail3" class="col-sm-3 control-label"><b
										class="red">*</b><strong>创建人</strong></label>
									<div class="col-sm-8">
										<input type="text" class="form-control" id="createUser">
									</div>
								</div>
								<div class="form-group" id="deptCTime">
									<label for="inputEmail3" class="col-sm-3 control-label"><b
										class="red">*</b><strong>创建时间</strong></label>
									<div class="col-sm-8">
										<input type="text" class="form-control" id="createTime">
									</div>
								</div>
								<div class="form-group" id="deptBut">
									<div class="col-sm-11" style="text-align: center;">
										<button type="button" class="btn btn-info btn-sm tooltip-info"
											onclick="subForm()">提交</button>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</form>
		</div>
	</div>
</body>
</html>